

class CheckStrLsFieldModelMixin:
    def get_y_ls(self, y_string):
        """
        有时候需要处理一下y字段
        :param y_string: 原来的str
        :return: 处理后的list
        """
        y_ls = y_string.replace('；', ';').split(';')
        return y_ls

    def check_str_ls_field_in_another_model(self, field_name, another_model, another_field_name):
        """
        如果self有一个字段`img_ls`是"1;3;5;2;4;", 且对应另一个模型MyImage的`id`字段, 则:
        self.check_str_ls_field_in_another_model(field_name="img_ls", another_model=MyImage, another_field_name="id")
        将检查`img_ls`中的每个小`img_i`否都在MyImage中有对应的对象

        :param field_name: 列表拼凑成的str字段, 以中英文分号隔开
        :param another_model: 对应的模型
        :param another_field_name: 对应模型中的字段
        """
        from bddjango import get_base_queryset
        from bddjango import get_field_names_by_model

        field_names = get_field_names_by_model(self)
        verbose_names = get_field_names_by_model(self, 'verbose_name')
        dc = dict(zip(field_names, verbose_names))
        verbose_name = dc.get(field_name)

        y_string = getattr(self, field_name)
        qs_ls = get_base_queryset(another_model)
        if y_string:
            y_ls = self.get_y_ls(y_string)
            for y_i in y_ls:
                y_i = y_i.strip()
                if y_i:
                    assert qs_ls.filter(**{another_field_name: y_i}).exists(), f'id[{self.pk}]的`{field_name}({verbose_name})`值[{y_i}]不存在对应对象!'

